<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>The File I/O library - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/tidylib.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">The File I/O library</h1></div></div></div>
  <p>The File I/O library we shall be using is very similar <a href="../../payload/copy_file2">to the one we saw earlier
in this tutorial</a>:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// You may remember this from the tutorial section on Custom Payloads
</span><span class="c1"></span><span class="k">namespace</span> <span class="n">filelib</span>
<span class="p">{</span>
  <span class="c1">// Error code + paths related to a failure. Also causes ADL discovery
</span><span class="c1"></span>  <span class="c1">// to check this namespace.
</span><span class="c1"></span>  <span class="k">struct</span> <span class="n">failure_info</span>
  <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">error_code</span> <span class="n">ec</span><span class="p">;</span>
    <span class="n">path</span> <span class="n">path1</span><span class="p">{},</span> <span class="n">path2</span><span class="p">{};</span>
  <span class="p">};</span>

  <span class="c1">// Tell Outcome that failure_info is to be treated as a std::error_code
</span><span class="c1"></span>  <span class="kr">inline</span> <span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span><span class="n">make_error_code</span><span class="p">(</span><span class="k">const</span> <span class="n">failure_info</span> <span class="o">&amp;</span><span class="n">fi</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">;</span> <span class="p">}</span>

  <span class="c1">// Tell Outcome that no-value observation should throw a custom exception
</span><span class="c1"></span>  <span class="kr">inline</span> <span class="kt">void</span> <span class="n">outcome_throw_as_system_error_with_payload</span><span class="p">(</span><span class="n">failure_info</span> <span class="n">fi</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="c1">// If the error code is not filesystem related e.g. ENOMEM, throw that
</span><span class="c1"></span>    <span class="c1">// as a standard STL exception.
</span><span class="c1"></span>    <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">try_throw_std_exception_from_error</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">);</span>
    <span class="c1">// Throw the exact same filesystem_error exception which the throwing
</span><span class="c1"></span>    <span class="c1">// copy_file() edition does.
</span><span class="c1"></span>    <span class="k">throw</span> <span class="nf">filesystem_error</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">.</span><span class="n">message</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">path1</span><span class="p">),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">path2</span><span class="p">),</span> <span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="c1">// Localise a result implementation specific to this namespace.
</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="o">&gt;</span> <span class="k">using</span> <span class="n">result</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">failure_info</span><span class="o">&gt;</span><span class="p">;</span>

  <span class="c1">// Writes a chunk of data to some file. Returns bytes written, or
</span><span class="c1"></span>  <span class="c1">// failure_info. Never throws exceptions.
</span><span class="c1"></span>  <span class="n">result</span><span class="o">&lt;</span><span class="n">size_t</span><span class="o">&gt;</span> <span class="n">write_file</span><span class="p">(</span><span class="n">string_view</span> <span class="n">chunk</span><span class="p">)</span> <span class="k">noexcept</span><span class="p">;</span>
<span class="p">}</span>  <span class="c1">// namespace filelib
</span><span class="c1"></span></code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L110" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>This uses the advanced Outcome feature of programming the lazy synthesis of
custom C++ exception throws from a payload carrying <code>E</code> type called <code>failure_info</code>.
Like the HTTP library, it too template aliases a localised <code>result</code> implementation
into its namespace with ADL bridging so Outcome customisation points can be
discovered.</p>


        </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/tidylib.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
